package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.GlobalExceptionHandler;
import com.itheima.common.R;
import com.itheima.dto.SetmealDto;
import com.itheima.pojo.Category;
import com.itheima.pojo.Setmeal;
import com.itheima.pojo.SetmealDish;
import com.itheima.service.CategoryService;
import com.itheima.service.SetmealDishService;
import com.itheima.service.SetmealService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    @Autowired
    private SetmealDishService setmealDishService;

    @Autowired
    private CategoryService categoryService;


    /**
     * @param setmealDto
     * @return
     */
    @PostMapping
    @CacheEvict(value = "setMealCache",allEntries = true)
    public R save(@RequestBody SetmealDto setmealDto) {
        setmealService.saveWithSetmealDish(setmealDto);
        return R.success("保存成功");
    }

    @GetMapping("/page")
    public R page(Integer page, Integer pageSize, String name) {
        IPage<Setmeal> pageInfo = new Page<>(page, pageSize);
        QueryWrapper<Setmeal> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(name), "name", name);
        //调用service分页查询
        IPage<Setmeal> result = setmealService.page(pageInfo, queryWrapper);
        //根据查询结果得到当前页数据--Setmeal集合
        List<Setmeal> setmeals = result.getRecords();
        //遍历集合--
        List<Setmeal> setmealDtos = setmeals.stream().map((setmeal) -> {
            //遍历集合--获得categoryId
            Long categoryId = setmeal.getCategoryId();
            //根据categoryId--category对象
            Category category = categoryService.getById(categoryId);
            //根据category对象---categoryName
            String categoryName = category.getName();
            //创建新的setmealDto对象
            SetmealDto setmealDto = new SetmealDto();
            //将setmeal的属性值copy到setmealDto中
            BeanUtils.copyProperties(setmeal, setmealDto);
            //设置setmealDto的categoryName
            setmealDto.setCategoryName(categoryName);
            //将新的setmealDto返回
            return setmealDto;
            //生成新的集合
        }).collect(Collectors.toList());
        //将返回结果封装的setmeal 替换成setmealDtos
        result.setRecords(setmealDtos);
        //将结果响应给前端
        return R.success(result);
    }

    @DeleteMapping
    @CacheEvict(value = "setMealCache",allEntries = true)
    public R delete(@RequestParam List<Long> ids) {
        R r = setmealService.deleteWithSetmealDish(ids);
        return r;
    }

    @GetMapping("/list")
    @Cacheable(value = "setmealCache",key ="#root.args[0]+'_'+#root.args[1]" )
    public R list(Long categoryId, Integer status){
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Setmeal::getStatus,status);
        queryWrapper.eq(Setmeal::getCategoryId,categoryId);
        List<Setmeal> list = setmealService.list(queryWrapper);
        return R.success(list);
    }
}
